<template>
	<view class="container">
	
		<uni-section title="当前兑换比例100:1" type="line">
			<view class="example">
				
				
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms >
					 <uni-forms-item label="当前拥有" required>
						 <text >{{baseData.income}}金币</text>
					
					 </uni-forms-item>
					<uni-forms-item label="银行名称" required>
						<uni-easyinput v-model="baseData.bankName" placeholder="银行名称" />
					</uni-forms-item>
					<uni-forms-item label="银行卡号" required>
						<uni-easyinput v-model="baseData.cardNo" type="number"  placeholder="银行卡号" />
					</uni-forms-item>
					<uni-forms-item label="姓名" required>
						<uni-easyinput v-model="baseData.cardUserName"  placeholder="银行卡号对应的姓名" />
					</uni-forms-item>
					<uni-forms-item label="兑换现金" required>
						<uni-easyinput v-model="baseData.expense" type="number" placeholder="要兑换现金的金币数" />
					</uni-forms-item>
					
					
					<button type="primary" @click="submit()">兑换</button>
				</uni-forms>
				
			</view>
		</uni-section>

	
		
		
	</view>
</template>
<script>
	import store from '@/store/index.js';//需要引入store
	import md5 from 'js-md5';
	export default {
		computed:{
			
			
		},
		onLoad(option) {
		
			
			if(option.userId!=undefined){
				this.baseData.userId=option.userId;
				this.baseData.income=option.income;
				
			}
			
					
			
		},
		data() {
			return {
				
				// 基础表单数据
				baseData: {
					userId:'',
					income:0,
					bankName: '',
					cardNo: '',
					cardUserName: '',
					expense: 0,
					
				},
			
			
			
			}
		},
	
		
		onReady() {
			
		},
		methods: {
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			
			submit() {
			
				if(this.$data.baseData.expense>1000){
					if(this.$data.baseData.income>=this.$data.baseData.expense){
						this.$data.baseData.income=this.$data.baseData.income-this.$data.baseData.expense;
						if(this.$data.baseData.bankName.length>0&&this.$data.baseData.cardNo.length>0&&this.$data.baseData.cardUserName.length>0&&this.$data.baseData.expense>0){
							let data={
								"userId":this.$data.baseData.userId,
								"bankName":this.$data.baseData.bankName,
								"cardNo":this.$data.baseData.cardNo,
								"cardUserName":this.$data.baseData.cardUserName,
								"expense":this.$data.baseData.expense
							
								
								
							}
							store.dispatch('addExpenseData',data).then(()=>{
								uni.showToast({
									icon: 'none',
									title: "提现已完成,请耐心等待付款" 
								})
								// uni.navigateTo({
								// 	url:"/pages/user/userInfo"
								// })
							});
						}else{
							uni.showToast({
								icon: 'none',
								title: "请完善提现信息" 
							})
						}
					}else{
						uni.showToast({
							icon: 'none',
							title: "账号金币不足" 
						})
					}
				}else{
					uni.showToast({
						icon: 'none',
						title: "兑换的金币需大于1000" 
					})
				}
				
				
				
				
			},
			editPwd(){
				
				
				let self=this;
				if(this.$data.basePwd.oldpwd.length>0&&this.$data.basePwd.pwd.length>0&&this.$data.basePwd.repwd.length>0){
					
					      
					if(this.$data.basePwd.pwd==this.$data.basePwd.repwd){
						const md5Value = md5(this.$data.basePwd.oldpwd);
						if(md5Value==this.$data.baseData.pwd){
							let data={
								"userId":this.$data.baseData.userId,
								"pwd":this.$data.basePwd.pwd,
								"oldPwd":this.$data.basePwd.oldpwd,
								
							}
							store.dispatch('editPwdData',data).then(()=>{
								
								uni.showToast({
									icon: 'none',
									title: "密码修改成功" 
								})
							
								
								
							});
						}else{
							uni.showToast({
								icon: 'none',
								title: "旧密码不正确" 
							})
						}
						
					}else{
						uni.showToast({
							icon: 'none',
							title: "新密码和确认密码不一致" 
						})
					}
				}else{
					uni.showToast({
						icon: 'none',
						title: "相关的密码字段不可以为空" 
					})
				}
				
			},
			logout(){
				uni.removeStorageSync('authInfo');
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		}
	}
</script>
<style lang="scss">

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>